<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Group extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
		if(!$this->auth->is_super_admin()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index()
	{
		$data['groups'] = $this->authGroup_m->get_all_groups();
		$this->load->view('group/index', $data);
	}

    public function edit($id)
    {
        if(!empty($id) && is_numeric($id)){
            $group = $this->authGroup_m->get_group_by_id($id);
            $rule = $this->authRule_m->get_rules_available();
            $group['rules'] = explode(',', $group['rules']);
            $rule = $this->getMenu($rule);
            $data["rule"] = $rule;
            $data["group"] = $group;
//            pp($data);exit();
            $this->load->view('group/form', $data);
        }else{
            echo "参数缺失";exit();
        }
    }

    public function update()
    {
        $post = $this->input->post ();
        $data['title'] = isset($post['title']) ? trim($post['title']) : false;
        $id = isset($post['id']) ? intval($post['id']) : false;
        if ($data['title']) {
            $status = isset($post['status']) ? $post['status'] : '';
            if ($status == 'on') {
                $data['status'] = 1;
            } else {
                $data['status'] = 0;
            }
            //如果是超级管理员一直都是启用状态
            if ($id == 1) {
                $data['status'] = 1;
            }

            $rules = isset($post['rules']) ? $post['rules'] : 0;
            if (is_array($rules)) {
                foreach ($rules as $k => $v) {
                    $rules[$k] = intval($v);
                }
                $rules = implode(',', $rules);
            }
            $data['rules'] = $rules;
            if ($id) {
                $group = $this->authGroup_m->sava_group_rule($id,$data);
                if ($group) {
                    show_message('修改成功',site_url(X_URL.'group/index'),1);
                } else {
                    show_message('修改失败',site_url(X_URL.'group/edit/'.$id),1);
                }
            } else {
                $group = $this->authGroup_m->add_group_rule();
                if ($group) {
                    show_message('新增成功',site_url(X_URL.'group/index'),1);
                } else {
                    show_message('新增失败',site_url(X_URL.'group/add'),1);
                }
            }
        } else {
            exit("用户组名称不能为空！");
        }
    }
    public function status($id)
    {
        if (!$id) {
            show_message('参数错误',site_url(X_URL.'group/index'),1);
        }
        if ($id == 1) {
            show_message('此用户组不可变更状态',site_url(X_URL.'group/index'),1);
        }
        $group = $this->authGroup_m->get_group_by_id($id);
        if (!$group) {
            show_message('参数错误，没有此用户组',site_url(X_URL.'group/index'),1);

        }
        $status = $group['status'];
        if ($status == 1) {
            $result = $this->authGroup_m->sava_group_rule($id,['status' => 0]);
        }
        if ($status != 1 ) {
            $result = $this->authGroup_m->sava_group_rule($id,['status' => 1]);
        }
        if ($result) {
            show_message('恭喜，更新状态成功',site_url(X_URL.'group/index'),1);
        } else {
            show_message('更新失败',site_url(X_URL.'group/index'),1);
        }
    }
}